﻿// CPatient.cpp: 实现文件
//

#include "pch.h"
#include "HospitalSystem.h"
#include "CPatient.h"
#include "patient.h"

// CPatient

IMPLEMENT_DYNCREATE(CPatient, CFormView)

CPatient::CPatient()
	: CFormView(IDD_CPatient)
	, m_add1(_T(""))
	, m_add2(_T(""))
	, m_add3(_T(""))
	, m_add4(0)
	, m_add5(_T(""))
	, m_add6(_T(""))
	, m_add7(_T(""))
	, m_add8(_T(""))
	, m_xiugai1(_T(""))
	, m_xiugai2(_T(""))
	, m_xiugai3(_T(""))
	, m_xiugai4(0)
	, m_xiugai5(_T(""))
	, m_xiugai6(_T(""))
	, m_xiugai7(_T(""))
	, m_xiugai8(_T(""))
	, m_delete(_T(""))
	, m_add9(_T(""))
	, m_xiugai9(_T(""))
{

}

CPatient::~CPatient()
{
}

void CPatient::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_ptlist);
	DDX_Text(pDX, IDC_EDIT2, m_add1);
	DDX_Text(pDX, IDC_EDIT1, m_add2);
	DDX_Text(pDX, IDC_EDIT6, m_add3);
	DDX_Text(pDX, IDC_EDIT3, m_add4);
	DDX_Text(pDX, IDC_EDIT5, m_add5);
	DDX_Text(pDX, IDC_EDIT7, m_add6);
	DDX_Text(pDX, IDC_EDIT14, m_add7);
	DDX_Text(pDX, IDC_EDIT17, m_add8);
	DDX_Text(pDX, IDC_EDIT10, m_xiugai1);
	DDX_Text(pDX, IDC_EDIT8, m_xiugai2);
	DDX_Text(pDX, IDC_EDIT12, m_xiugai3);
	DDX_Text(pDX, IDC_EDIT11, m_xiugai4);
	DDX_Text(pDX, IDC_EDIT13, m_xiugai5);
	DDX_Text(pDX, IDC_EDIT15, m_xiugai6);
	DDX_Text(pDX, IDC_EDIT16, m_xiugai7);
	DDX_Text(pDX, IDC_EDIT18, m_xiugai8);
	//  DDX_Text(pDX, IDC_EDIT9, m_delete);
	DDX_Text(pDX, IDC_EDIT9, m_delete);
	DDX_Text(pDX, IDC_EDIT20, m_add9);
	DDX_Text(pDX, IDC_EDIT19, m_xiugai9);
}

BEGIN_MESSAGE_MAP(CPatient, CFormView)
	ON_BN_CLICKED(IDC_BUTTON5, &CPatient::OnBnClickedButton5)
	ON_EN_CHANGE(IDC_EDIT19, &CPatient::OnEnChangeEdit19)
	ON_BN_CLICKED(IDC_BUTTON10, &CPatient::OnBnClickedButton10)
	ON_BN_CLICKED(IDC_BUTTON6, &CPatient::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON7, &CPatient::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON8, &CPatient::OnBnClickedButton8)
	ON_BN_CLICKED(IDC_BUTTON9, &CPatient::OnBnClickedButton9)
END_MESSAGE_MAP()


// CPatient 诊断

#ifdef _DEBUG
void CPatient::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CPatient::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CPatient 消息处理程序


void CPatient::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();

	// TODO: 在此添加专用代码和/或调用基类
	CFormView::OnInitialUpdate();
	CString field[] = { _T("身份证号"), _T("姓名"), _T("性别"), _T("年龄"), _T("病历信息"), _T("入院日期"), _T("出院日期") };
	for (int i = 0; i < sizeof(field) / sizeof(field[0]); ++i)
	{
		m_ptlist.InsertColumn(i, field[i], LVCFMT_CENTER, 80);
	}
	//设置正文
	CString strsql;
	strsql.Format(L"select * from patient");
	List_All(strsql);
	// TODO: 在此添加专用代码和/或调用基类
}


void CPatient::OnBnClickedButton5()
{
	// TODO: 在此添加控件通知处理程序代码
	Patient m_cd;
	if (m_cd.IsOpen())
	{
		m_cd.Close();
	}
	m_cd.Open(CRecordset::dynaset);
	UpdateData(true);
	while (!m_cd.IsEOF())
	{
		
		if (m_add1 == m_cd.m_pt_id)
		{
			MessageBox(L"改患者已存在！");
			return;
		}
		m_cd.MoveNext();
	}
	m_cd.AddNew();
	m_cd.m_pt_id = m_add1;
	m_cd.m_pt_name = m_add2;
	m_cd.m_pt_sex = m_add3;//年龄与性别写反
	m_cd.m_pt_age = m_add4;
	m_cd.m_pt_md_rd = m_add5;
	m_cd.m_pt_zy_date = m_add6;
	m_cd.m_pt_cy_date = m_add7;
	m_cd.m_wd_id = m_add8;
	m_cd.m_dr_id = m_add9;
	if (m_cd.Update())
	{
		MessageBox(L"添加成功！");
		CString strsql;
		strsql.Format(L"select * from patient");
		List_All(strsql);
	}
	else
	{
		MessageBox(L"添加失败！");
	}
	
	m_cd.Close();
}


void CPatient::OnEnChangeEdit19()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CFormView::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
}


void CPatient::OnBnClickedButton10()
{
	// TODO: 在此添加控件通知处理程序代码
	/*Patient m_cd;
	m_cd.Open(CRecordset::dynaset);
	UpdateData(true);
	m_cd.MoveFirst();
	while (!m_cd.IsEOF())
	{
		if (m_cd.IsOpen())
		{
			m_cd.Close();
		}
		m_cd.Open(CRecordset::dynaset);
			m_cd.Delete();
			
			//MessageBox(L"删除成功！");
			
		m_cd.MoveNext();
		//m_cd.Close();
	}*/

	m_delete.Empty();
	UpdateData(FALSE); //把数据更新到控件上

}

void CPatient::List_All(CString str=L"select * from patient") {
	m_ptlist.DeleteAllItems();
	//Cscore m_score;
	Patient m_pt;
	try {
		if (m_pt.IsOpen())
			m_pt.Close();
		if (!m_pt.Open(CRecordset::snapshot, str)) {
			MessageBox(L"打开数据库失败", L"数据库错误", MB_OK);
			return;
		}
	}
	catch (CDBException* e) {
		e->ReportError();
	}

	int nindex = 0;
	m_pt.MoveFirst();
	CString age;

	while (!m_pt.IsEOF()) {
		LV_ITEM litem;
		litem.mask = LVIF_TEXT;
		litem.iItem = nindex;
		litem.iSubItem = 0;
		litem.pszText = L"";

		m_ptlist.InsertItem(&litem);
		age.Format(L"%3d", m_pt.m_pt_age);
		m_ptlist.SetItemText(nindex, 0, m_pt.m_pt_id);
		m_ptlist.SetItemText(nindex, 1, m_pt.m_pt_name);
		m_ptlist.SetItemText(nindex, 2, m_pt.m_pt_sex);
		m_ptlist.SetItemText(nindex, 3, age);
		m_ptlist.SetItemText(nindex, 4, m_pt.m_pt_md_rd);
		m_ptlist.SetItemText(nindex, 5, m_pt.m_pt_zy_date);
		m_ptlist.SetItemText(nindex, 6, m_pt.m_pt_cy_date);
		m_ptlist.SetItemText(nindex, 7, m_pt.m_dr_id);
		m_ptlist.SetItemText(nindex, 8, m_pt.m_wd_id);
		m_pt.MoveNext();
		nindex++;
	}
	m_pt.Close();
}


void CPatient::OnBnClickedButton6()
{
	/*//添加病人信息
	Patient m_cd;
	int j = 0, k = 10, m = 0, n = 0;
	for (int i = 100; i < 200; i++)
	{
		if (m_cd.IsOpen())
		{
			m_cd.Close();
		}
		m_cd.Open(CRecordset::dynaset);
		m_cd.AddNew();
		
		CString name, age, id, wd, dr;
		id.Format(TEXT("500384200109097%d"), i);
		j++;
		name.Format(TEXT("张三_%d"), j);
		m_cd.AddNew();
		m_cd.m_pt_id = id;
		m_cd.m_pt_name = name;
		if (k > 88)
		{
			k = 1;
		}
		k = k + 9;
		if (i % 2)
		{
			m_cd.m_pt_sex = L"男";
		}
		else
		{
			m_cd.m_pt_sex = L"女";
		}
		
		m_cd.m_pt_age = k;
		if (i % 5 == 0)
		{
			m_cd.m_pt_md_rd = L"心脏病";
		}
		if (i % 5 == 1)
		{
			m_cd.m_pt_md_rd = L"感冒";
		}
		if (i % 5 == 2)
		{
			m_cd.m_pt_md_rd = L"糖尿病";
		}
		if (i % 5 == 3)
		{
			m_cd.m_pt_md_rd = L"骨折";
		}
		if (i % 5 == 4)
		{
			m_cd.m_pt_md_rd = L"慢性胃炎";
		}
		m_cd.m_pt_zy_date = L"2021-12-27";
		if (i % 2)
		{
			m_cd.m_pt_cy_date = L"未出院";
		}
		else
		{
			m_cd.m_pt_cy_date = L"2022-01-01";
		}
		if (m >41)
			m = 5;
		m = m + 1;
		if (m< 10)
		{
			
			wd.Format(TEXT("R00%d"), m);
		}
	
		if (m> 9&&m<42)
		{
		
			wd.Format(TEXT("R0%d"), m);
		}
		m_cd.m_wd_id = wd;
		if (n>22)
			n = 3;
		n = n + 1;
		if (n< 10)
		{
			
			dr.Format(TEXT("Y00%d"), n);
		}

		if (n>9&&n<22)
		{
			dr.Format(TEXT("Y0%d"), n);
		}
		m_cd.m_dr_id = dr;
		m_cd.Update();
		m_cd.Close();
	}
	*/
	// TODO: 在此添加控件通知处理程序代码
	m_add2.Empty();
	m_add1.Empty();
	m_add3.Empty();
	m_add9.Empty();
	m_add5.Empty();
	m_add6.Empty();
	m_add7.Empty();
	m_add8.Empty();
	UpdateData(FALSE); //把数据更新到控件上
}


void CPatient::OnBnClickedButton7()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(true);
	Patient m_cd;
	m_cd.Open(CRecordset::dynaset);
	if (!m_cd.CanUpdate())
	{
		MessageBox(L"无权限！");
		return;
	}
	m_cd.MoveFirst();
	while (!m_cd.IsEOF())
	{
		
		if (m_xiugai1 == m_cd.m_pt_id)
		{
			m_cd.Edit();
			m_cd.m_pt_name = m_xiugai2;
			m_cd.m_pt_sex = m_xiugai3;
			m_cd.m_pt_age = m_xiugai4;
			m_cd.m_pt_md_rd = m_xiugai5;
			m_cd.m_pt_zy_date = m_xiugai6;
			m_cd.m_pt_cy_date = m_xiugai7;
			m_cd.m_dr_id = m_xiugai9;
			m_cd.m_wd_id = m_xiugai8;
			m_cd.Update();
			MessageBox(L"修改成功");
			CString strsql;
			strsql.Format(L"select * from patient");
			List_All(strsql);
			return;
		}
		m_cd.MoveNext();
	}

	MessageBox(L"不存在改患者！");
}


void CPatient::OnBnClickedButton8()
{
	// TODO: 在此添加控件通知处理程序代码
	m_xiugai2.Empty();
	m_xiugai1.Empty();
	m_xiugai3.Empty();
	m_xiugai5.Empty();
	m_xiugai6.Empty();
	m_xiugai7.Empty();
	m_xiugai8.Empty();
	m_xiugai9.Empty();
	UpdateData(FALSE); //把数据更新到控件上
}


void CPatient::OnBnClickedButton9()
{
	// TODO: 在此添加控件通知处理程序代码
	Patient m_cd;
	m_cd.Open(CRecordset::dynaset);
	UpdateData(true);
	m_cd.MoveFirst();
	while (!m_cd.IsEOF())
	{
		if (m_cd.m_pt_id == m_delete)
		{
			m_cd.Delete();
			m_cd.Close();
			MessageBox(L"删除成功！");
			CString strsql;
			strsql.Format(L"select * from patient");
			List_All(strsql);
			return;
		}
		m_cd.MoveNext();
	}
	MessageBox(L"不存改患者，删除失败！");
}
